package com.siecomzg.transport.handler;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Process;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.siecomzg.bean.IdentityInfoBean;
import com.siecomzg.constconfig.ConstDefine;
import com.siecomzg.proto.Message;
import com.siecomzg.tool.ByteTool;
import com.siecomzg.tool.LogUtil;
import com.siecomzg.transport.CallbackMap;
import com.siecomzg.transport.CallbackTask;
import com.siecomzg.transport.CommonClient;
import com.siecomzg.transport.DeviceCallback;
import com.siecomzg.transport.SiecomDevice;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;

import sdses.id2piclib.wltToBmpInterface;

//import sdses.id2piclib.wltToBmpInterface;

/**
 * Created  on 2016/7/28.
 */
public class IdentityHandler extends CommonHandler {
    private static int errCode = 0;
    private static CallbackMap callbackMap = CallbackMap.getInstance();

    DeviceCallback tempCallBack = null;
    @Override
    public void onReceive(CommonClient server, Message.siecomproto msg) {

        String msgid = msg.getMsgid();
        HashMap<String, CallbackTask> callbackTaskHashMap = CallbackMap.getCallbackTaskHashMap();
        if (callbackTaskHashMap.containsKey(msgid)) {
            CallbackTask callbackTask = callbackTaskHashMap.get(msgid);
            DeviceCallback callback = callbackTask.getCallback();
            callbackTask.getTimer().cancel();
            boolean isOnMain = callbackTask.isOnMain();
            if (msg.getCmd() == ConstDefine.IDENTITY_CANCEL_RESPOND) {
                cancelCall(msg, callback, isOnMain);
                callbackTaskHashMap.remove(msgid);
            }
            if (msg.getCmd() == ConstDefine.ID_RESPOND) {
                readIdentityByRemote(msg, callback, isOnMain);
                callbackTaskHashMap.remove(msgid);
            }
        }

    }

    private void cancelCall(Message.siecomproto msg, DeviceCallback callback, boolean isOnMain) {
        int errno = msg.getError();
        if (errno == 0) {
            Bundle bundle = new Bundle();
            bundle.putString("data", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
        } else {
            Bundle bundle = new Bundle();
            bundle.putInt("code", errno);
            bundle.putString("message", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);

        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);
    }

    public static final String TAG = IdentityHandler.class.getSimpleName();

    private void readIdentityByRemote(Message.siecomproto msg, DeviceCallback callback, boolean isOnMain) {
        int errno = msg.getError();
        if (errno == 0) {
            Bundle bundle = new Bundle();
            Gson gson = new Gson();
            IdentityInfoBean bean = gson.fromJson(msg.getRespond(), IdentityInfoBean.class);

            byte[] head = new byte[1024];
            if(TextUtils.isEmpty(bean.imageStr) && !msg.getExtion().isEmpty()){
                byte[] all = msg.getExtion().toByteArray();
                if (all.length >= 1280)
                    System.arraycopy(all,256,head,0,1024);

            }else{
                head = ByteTool.hexStr2Bytes(bean.imageStr);
            }
            bean.icon = parsePhotoOther(head);
//            bean.icon = Tool.getBitmap(head);  //更换解码库


//            bean.icon = getPicFromBytes(ByteTool.hexStr2Bytes(bean.imageStr), null);
            bundle.putParcelable("data", bean);
            bundle.putString("orgData",msg.getRequest());
            bundle.putString("dataJson",msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
        } else {
            Bundle bundle = new Bundle();
            bundle.putInt("code", errno);
            bundle.putString("message", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);

        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);


    }

    public static Bitmap getPicFromBytes(byte[] bytes,
                                         BitmapFactory.Options opts) {
        if (bytes != null)
            if (opts != null)
                return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,
                        opts);
            else
                return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
        return null;
    }

    public static int getPicByBuff(String packageName, byte[] wltBuff, byte[] bmpBuff) {
        String head = "424DCE970000000000003600000028000000660000007E00000001001800000000000000000000000000000000000000000000000000";
        byte[] tmpBmpBuff1 = new byte['韎'];
        byte[] tmpBmpBuff2 = new byte['韎'];
        byte[] headbuff = new byte[54];
        headbuff = ByteTool.hexStr2Bytes(head);
        int nRet = 0;
         nRet = wltToBmpInterface.wltToBmp(packageName, wltBuff, tmpBmpBuff1);
        if (nRet != 1) {
            nRet = wltToBmpInterface.wltToBmp(packageName, wltBuff, tmpBmpBuff1);
        }

        int hLen = head.length() / 2;
        System.arraycopy(headbuff, 0, bmpBuff, 0, hLen);

        int i;
        for (i = 0; i < 12954; ++i) {
            tmpBmpBuff2[i * 3 + 0] = tmpBmpBuff1[i * 3 + 2];
            tmpBmpBuff2[i * 3 + 1] = tmpBmpBuff1[i * 3 + 1];
            tmpBmpBuff2[i * 3 + 2] = tmpBmpBuff1[i * 3 + 0];
        }

        for (i = 0; i < 126; ++i) {
            System.arraycopy(tmpBmpBuff2, i * 306, bmpBuff, hLen + i * 308, 306);
            bmpBuff[hLen + i * 308 + 306 + 0] = 0;
            bmpBuff[hLen + i * 308 + 306 + 1] = 0;
        }

        LogUtil.i("ComShell", "picUnpack nRet is:" + nRet);
        return nRet;
    }

    public static Bitmap parsePhotoOther(byte[] recData) {
//		return DecodeWltUtil.decode(mContext,recData, true, Task.IDPhotoParseFunction.THIRD_GENERATION);
        byte[] bmp = new byte['韎'];
        String path = Environment.getExternalStorageDirectory() + "/Android/wltlib";
        String appPackageName = getAppPackageName();
        if (appPackageName == null) {
            return null;
        } else {
            errCode = getPicByBuff(appPackageName, recData, bmp);
            if (errCode == 1) {
                Bitmap bitmap = BitmapFactory.decodeByteArray(bmp, 0, bmp.length);
                if (true) {
                    String imageFilePath = path;

                    try {
                        File imageFile = new File(imageFilePath);
                        if (imageFile.exists() && !imageFile.delete()) {
                            Log.e("warning", imageFilePath + "delete fail");
                        }

                        if (!imageFile.getParentFile().exists()) {
                            imageFile.getParentFile().mkdirs();
                        }

                        FileOutputStream fileOutputStream = new FileOutputStream(imageFilePath);
                        fileOutputStream.write(bmp);
                        fileOutputStream.flush();
                        fileOutputStream.close();
                    } catch (IOException var8) {
                        var8.printStackTrace();
                    }
                }

                return bitmap;
            } else {
                return null;
            }
        }
    }

    public static String getAppPackageName() {
        int pid = Process.myPid();
        File file = new File("/proc/" + pid + "/cmdline");
        if(!file.exists()) {
            return null;
        } else {
            FileInputStream fileInputStream = null;

            try {
                fileInputStream = new FileInputStream(file);
                byte[] buffer = new byte[64];
                int len = fileInputStream.read(buffer);
                if(len <= 0) {
                    return null;
                } else {
                    int i;
                    for(i = 0; i < buffer.length && buffer[i] != 0; ++i) {
                        ;
                    }

                    String var7 = new String(buffer, 0, i);
                    return var7;
                }
            } catch (IOException var16) {
                Log.e("getAppPackageName()", "从文件读取信息是发生IO异常");
                var16.printStackTrace();
                return null;
            } finally {
                if(fileInputStream != null) {
                    try {
                        fileInputStream.close();
                    } catch (IOException var15) {
                        Log.e("getAppPackageName()", "关闭fileInputStream时发生IO异常");
                        var15.printStackTrace();
                    }
                }

            }
        }
    }

    /**
     * 解析身份证数据
     * @param data
     * @return
     */
    public IdentityInfoBean parseIdInfo(byte[] data){
        IdentityInfoBean people = new IdentityInfoBean();
        try {
            byte[] dataBuf = new byte[256];

//            byte[] head = new byte[1024];
            if (data.length >=  1280){
                System.arraycopy(data,0,dataBuf,0,256);
//                System.arraycopy(data,256,head,0,1024);
            }

            byte[] finger = null;
            if(data.length >= ( 256 + 1024 + 1024)){
                finger = new byte[1024];
                System.arraycopy(data,256+1024,finger,0,1024);
            }

            String TmpStr = new String(dataBuf, StandardCharsets.UTF_16LE);
            LogUtil.e(TAG,"TmpStr--" + TmpStr);

            String[] decodeInfo = new String[12];
            String cardType = TmpStr.substring(124, 125);
            LogUtil.e(TAG,"cardType==" + cardType);
            if(cardType.equals("I")) {

                String enName = new String(dataBuf, 0, 120, "UTF-16LE").trim();
                Log.e("enName",enName);

                String fprSex = new String(dataBuf, 120, 2, "UTF-16LE").trim();
                String frpIDNo = new String(dataBuf, 122, 30, "UTF-16LE").trim();
                Log.e("frpIDNo",frpIDNo);

                String countryCode = new String(dataBuf, 152, 6, "UTF-16LE").trim();
                Log.e("countryCode",countryCode);

                String CHNName = new String(dataBuf, 158, 30, "UTF-16LE").trim();
                Log.e("CHNName",CHNName);

                String validStart = new String(dataBuf, 188, 16, "UTF-16LE").trim();
                Log.e("validStart",validStart);

                String validEnd = new String(dataBuf, 204, 16, "UTF-16LE").trim();
                Log.e("validEnd",validEnd);

                String Birthday = new String(dataBuf, 220, 16, "UTF-16LE").trim();
                Log.e("Birthday",Birthday);

                String idVer = new String(dataBuf, 236, 4, "UTF-16LE").trim();
                Log.e("idVer",idVer);

                String issuingAuthorityCode = new String(dataBuf, 240, 8, "UTF-16LE").trim();
                Log.e("issuingAuthorityCode",issuingAuthorityCode);

                people.fullName = CHNName;
                people.birthday = Birthday;
                people.idNo = frpIDNo;
                people.idOrg = issuingAuthorityCode;
                people.beginDate = validStart;
                people.endDate = validEnd;
                //  bean.idAddr = m_strAddress;
                people.countryCode= countryCode;
                people.nation = countryMap.get(countryCode);
                if (fprSex.equals("1")){
                    people.sex  = "男";
                } else{
                    people.sex  = "女";
                }
                people.cardType = "I";
                people.cardVer = idVer;
                people.enName = enName;

            }else if (cardType.equals("J")){
                String GAT_Name = (new String(dataBuf, 0, 30, "UTF-16LE")).trim();
                Log.e(TAG,"GAT_NAME="+GAT_Name);
                String GAT_SexCode = (new String(dataBuf, 30, 2, "UTF-16LE")).trim();
                Log.e(TAG,"GAT_SexCode="+GAT_SexCode);
                String ReserveA = (new String(dataBuf, 32, 4, "UTF-16LE")).trim();
                Log.e(TAG,"ReserveA="+ReserveA);
                String GAT_Birthday = (new String(dataBuf, 36, 16, "UTF-16LE")).trim();
                Log.e(TAG,"GAT_Birthday="+GAT_Birthday);
                String GAT_Address = (new String(dataBuf, 52, 70, "UTF-16LE")).trim();
                Log.e(TAG,"GAT_Address="+GAT_Address);
                String GAT_IDNo = (new String(dataBuf, 122, 36, "UTF-16LE")).trim();
                Log.e(TAG,"GAT_IDNo="+GAT_IDNo);
                String GAT_IssuingAuthority = (new String(dataBuf, 158, 30, "UTF-16LE")).trim();
                Log.e(TAG,"GAT_IssuingAuthority="+GAT_IssuingAuthority);
                String GAT_ValidStartDate = (new String(dataBuf, 188, 16, "UTF-16LE")).trim();
                Log.e(TAG,"GAT_ValidStartDate="+GAT_ValidStartDate);
                String GAT_ValidEndDate = (new String(dataBuf, 204, 16, "UTF-16LE")).trim();
                Log.e(TAG,"GAT_ValidEndDate="+GAT_ValidEndDate);
                String GAT_PassportNo = (new String(dataBuf, 220, 18, "UTF-16LE")).trim();
                Log.e(TAG,"GAT_PassportNo="+GAT_PassportNo);
                String GAT_IssuingNo = (new String(dataBuf, 238, 4, "UTF-16LE")).trim();
                Log.e(TAG,"GAT_IssuingNo="+GAT_IssuingNo);
                String ReserveB = (new String(dataBuf, 242, 6, "UTF-16LE")).trim();
                Log.e(TAG,"ReserveB="+ReserveB);
                String ReserveC = (new String(dataBuf, 250, 6, "UTF-16LE")).trim();
                Log.e(TAG,"ReserveC="+ReserveC);

                String Gender;
                String GAT_Sex;
                if(GAT_SexCode.equals("1")) {
                    Gender = "男";
                    GAT_Sex = "男";
                } else {
                    Gender = "女";
                    GAT_Sex = "女";
                }


                people.fullName = GAT_Name;
//                bean.icon = decodePhoto(recvBuf);
                people.birthday = GAT_Birthday;
                people.idNo = GAT_IDNo;
                people.idOrg = GAT_IssuingAuthority;
                people.beginDate = GAT_ValidStartDate;
                people.endDate = GAT_ValidEndDate;
                people.idAddr = GAT_Address;
                people.sex = Gender;
                people.cardType = "J";
                people.reserveA = ReserveA;
                people.reserveB = ReserveB;
                people.reserveC = ReserveC;
                people.issuingNo= GAT_IssuingNo;

                people.passportNo= GAT_PassportNo;


            }else {
                decodeInfo[0] = TmpStr.substring(0, 15); //name
                decodeInfo[1] = TmpStr.substring(15, 16); // gender(1)
                decodeInfo[2] = TmpStr.substring(16, 18); // nation (2)
                decodeInfo[3] = TmpStr.substring(18, 26); // birthday (8)
                decodeInfo[4] = TmpStr.substring(26, 61); // address
                decodeInfo[5] = TmpStr.substring(61, 79); //id (18)
                decodeInfo[6] = TmpStr.substring(79, 94); //depart (15)
                decodeInfo[7] = TmpStr.substring(94, 102); // tsYYMMDD
                decodeInfo[8] = TmpStr.substring(102, 110); //tpYYMMDD
                decodeInfo[9] = TmpStr.substring(110, 119);
                decodeInfo[10] = TmpStr.substring(119, 121);
                decodeInfo[11] = TmpStr.substring(124, 125);


                if (decodeInfo[1].equals("1"))
                    decodeInfo[1] = "男";
                else
                    decodeInfo[1] = "女";

                String nation = "";
                if(!decodeInfo[2].equals("  ")) {
                    int code = Integer.parseInt(decodeInfo[2]);
                    LogUtil.e(TAG,"民族code==" + code);
                    nation = parseNation(code);
                }

                // 姓名
                people.setFullName(decodeInfo[0].trim());
                // 性别
                people.setSex(decodeInfo[1].trim());
                // 民族
                people.setNation(nation.trim());
                // 出生
                people.setBirthday(decodeInfo[3].trim());
                // 住址
                people.setIdAddr(decodeInfo[4].trim());
                // 身份证号
                people.setIdNo(decodeInfo[5].trim());
                // 签发机关
                people.setIdOrg(decodeInfo[6].trim());
                // 有效起始日期
                people.setBeginDate(decodeInfo[7].trim());
                // 有效截止日期
                people.setEndDate(decodeInfo[8].trim());
                people.setCardType("A");
                if(finger != null){
//                            people.setFingerByte(finger);
                    people.setFingerStr(ByteTool.byte2hex(finger));
//                            LogUtil.e(TAG,"指纹数据：" +ByteTool.byte2hex(people.getFingerByte())  );
                }

            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return people;
    }
    final static HashMap<String, String> countryMap = new HashMap<String, String>();

    static {
        countryMap.put("AFG", "阿富汗");
        countryMap.put("ALB", "阿尔巴尼亚");
        countryMap.put("DZA", "阿尔及利亚");
        countryMap.put("ASM", "美属萨摩亚");
        countryMap.put("AND", "安道尔");
        countryMap.put("AGO", "安哥拉");
        countryMap.put("AIA", "安圭拉");
        countryMap.put("ATA", "南极洲");
        countryMap.put("ATG", "安提瓜和巴布达");
        countryMap.put("ARG", "阿根廷");
        countryMap.put("ARM", "亚美尼亚");
        countryMap.put("ABW", "阿鲁巴");
        countryMap.put("AUS", "澳大利亚");
        countryMap.put("AUT", "奥地利");
        countryMap.put("AZE", "阿塞拜疆");
        countryMap.put("BHS", "巴哈马");
        countryMap.put("BHR", "巴林");
        countryMap.put("BGD", "孟加拉国");
        countryMap.put("BRB", "巴巴多斯");
        countryMap.put("BLR", "白俄罗斯");
        countryMap.put("BEL", "比利时");
        countryMap.put("BLZ", "伯利兹");
        countryMap.put("BEN", "贝宁");
        countryMap.put("BMU", "百慕大");
        countryMap.put("BTN", "不丹");
        countryMap.put("BOL", "玻利维亚");
        countryMap.put("BIH", "波黑");
        countryMap.put("BWA", "博茨瓦纳");
        countryMap.put("BVT", "布维岛");
        countryMap.put("BRA", "巴西");
        countryMap.put("IOT", "英属印度洋领土");
        countryMap.put("BRN", "文莱");
        countryMap.put("BGR", "保加利亚");
        countryMap.put("BFA", "布基纳法索");
        countryMap.put("BDI", "布隆迪");
        countryMap.put("KHM", "柬埔寨");
        countryMap.put("CMR", "喀麦隆");
        countryMap.put("CAN", "加拿大");
        countryMap.put("CPV", "佛得角");
        countryMap.put("CYM", "开曼群岛");
        countryMap.put("CAF", "中非");
        countryMap.put("TCD", "乍得");
        countryMap.put("CHL", "智利");
        countryMap.put("CHN", "中国");
        countryMap.put("HKG", "香港");
        countryMap.put("MAC", "澳门");
        countryMap.put("TWN", "台湾");
        countryMap.put("CSR", "圣诞岛");
        countryMap.put("CCK", "科科斯(基林)群岛");
        countryMap.put("COL", "哥伦比亚");
        countryMap.put("COM", "科摩罗");
        countryMap.put("COG", "刚果（布）");
        countryMap.put("COD", "刚果（金）");
        countryMap.put("COK", "库克群岛");
        countryMap.put("CR", "哥斯达黎加");
        countryMap.put("CIV", "科特迪瓦");
        countryMap.put("HRV", "克罗地亚");
        countryMap.put("CUB", "古巴");
        countryMap.put("CYP", "塞浦路斯");
        countryMap.put("CZE", "捷克");
        countryMap.put("DNK", "丹麦");
        countryMap.put("DJI", "吉布提");
        countryMap.put("DMA", "多米尼克");
        countryMap.put("DOM", "多米尼加共和国");
        countryMap.put("TMP", "东帝汶");
        countryMap.put("ECU", "厄瓜多尔");
        countryMap.put("EGY", "埃及");
        countryMap.put("SLV", "萨尔瓦多");
        countryMap.put("GNQ", "赤道几内亚");
        countryMap.put("ERI", "厄立特里亚");
        countryMap.put("EST", "爱沙尼亚");
        countryMap.put("ETH", "埃塞俄比亚");
        countryMap.put("FLK", "福克兰群岛(马尔维纳斯)");
        countryMap.put("FRO", "法罗群岛");
        countryMap.put("FJI", "斐济");
        countryMap.put("FIN", "芬兰");
        countryMap.put("FRA", "法国");
        countryMap.put("GUF", "法属圭亚那");
        countryMap.put("PYF", "法属波利尼西亚");
        countryMap.put("ATF", "法属南部领土");
        countryMap.put("GAB", "加蓬");
        countryMap.put("GMB", "冈比亚Gambia");
        countryMap.put("GEO", "格鲁吉亚");
        countryMap.put("DEU", "德国");
        countryMap.put("GHA", "加纳");
        countryMap.put("GIB", "直布罗陀");
        countryMap.put("GRC", "希腊");
        countryMap.put("GRL", "格陵兰");
        countryMap.put("GRD", "格林纳达");
        countryMap.put("GLP", "瓜德罗普");
        countryMap.put("GUM", "关岛");
        countryMap.put("GTM", "危地马拉");
        countryMap.put("GIN", "几内亚");
        countryMap.put("GNB", "几内亚比绍");
        countryMap.put("GUY", "圭亚那");
        countryMap.put("HTI", "海地");
        countryMap.put("HMD", "赫德岛和麦克唐纳岛");
        countryMap.put("HND", "洪都拉斯");
        countryMap.put("HUN", "匈牙利");
        countryMap.put("ISL", "冰岛");
        countryMap.put("IND", "印度");
        countryMap.put("IDN", "印度尼西亚");
        countryMap.put("IRN", "伊朗");
        countryMap.put("IRQ", "伊拉克");
        countryMap.put("IRL", "爱尔兰");
        countryMap.put("ISR", "以色列");
        countryMap.put("ITA", "意大利");
        countryMap.put("JAM", "牙买加");
        countryMap.put("JPN", "日本");
        countryMap.put("JOR", "约旦");
        countryMap.put("KAZ", "哈萨克斯坦");
        countryMap.put("KEN", "肯尼亚");
        countryMap.put("KIR", "基里巴斯");
        countryMap.put("PRK", "朝鲜");
        countryMap.put("KOR", "韩国");
        countryMap.put("KWT", "科威特");
        countryMap.put("KGZ", "吉尔吉斯斯坦");
        countryMap.put("LAO", "老挝");
        countryMap.put("LVA", "拉脱维亚");
        countryMap.put("LBN", "黎巴嫩");
        countryMap.put("LSO", "莱索托");
        countryMap.put("LBR", "利比里亚");
        countryMap.put("LBY", "利比亚");
        countryMap.put("LIE", "列支敦士登");
        countryMap.put("LTU", "立陶宛");
        countryMap.put("LUX", "卢森堡");
        countryMap.put("MKD", "前南马其顿");
        countryMap.put("MDG", "马达加斯加");
        countryMap.put("MWI", "马拉维");
        countryMap.put("MYS", "马来西亚");
        countryMap.put("MDV", "马尔代夫");
        countryMap.put("MLI", "马里");
        countryMap.put("MLT", "马耳他");
        countryMap.put("MHL", "马绍尔群岛");
        countryMap.put("MTQ", "马提尼克");
        countryMap.put("MRT", "毛里塔尼亚");
        countryMap.put("MUS", "毛里求斯");
        countryMap.put("MYT", "马约特");
        countryMap.put("MEX", "墨西哥");
        countryMap.put("FSM", "密克罗尼西亚联邦");
        countryMap.put("MDA", "摩尔多瓦");
        countryMap.put("MCO", "摩纳哥");
        countryMap.put("MNG", "蒙古");
        countryMap.put("MSR", "蒙特塞拉特");
        countryMap.put("MAR", "摩洛哥");
        countryMap.put("MOZ", "莫桑比克");
        countryMap.put("MMR", "缅甸");
        countryMap.put("NAM", "纳米比亚");
        countryMap.put("NRU", "瑙鲁");
        countryMap.put("NPL", "尼泊尔");
        countryMap.put("NLD", "荷兰");
        countryMap.put("ANT", "荷属安的列斯");
        countryMap.put("NCL", "新喀里多尼亚");
        countryMap.put("NZL", "新西兰");
        countryMap.put("NIC", "尼加拉瓜");
        countryMap.put("NER", "尼日尔");
        countryMap.put("NGA", "尼日利亚");
        countryMap.put("NIU", "纽埃");
        countryMap.put("NFK", "诺福克岛");
        countryMap.put("MNP", "北马里亚纳");
        countryMap.put("NOR", "挪威");
        countryMap.put("OMN", "阿曼");
        countryMap.put("PAK", "巴基斯坦");
        countryMap.put("PLW", "帕劳");
        countryMap.put("PST", "巴勒斯坦");
        countryMap.put("PAN", "巴拿马");
        countryMap.put("PNG", "巴布亚新几内亚");
        countryMap.put("PRY", "巴拉圭");
        countryMap.put("PER", "秘鲁");
        countryMap.put("PHL", "菲律宾");
        countryMap.put("PCN", "皮特凯恩群岛");
        countryMap.put("POL", "波兰");
        countryMap.put("PRT", "葡萄牙");
        countryMap.put("PRI", "波多黎各");
        countryMap.put("QAT", "卡塔尔");
        countryMap.put("REU", "留尼汪");
        countryMap.put("ROM", "罗马尼亚");
        countryMap.put("RUS", "俄罗斯联邦");
        countryMap.put("RWA", "卢旺达");
        countryMap.put("SHN", "圣赫勒拿");
        countryMap.put("KNA", "圣基茨和尼维斯");
        countryMap.put("LCA", "圣卢西亚");
        countryMap.put("SPM", "圣皮埃尔和密克隆");
        countryMap.put("VCT", "圣文森特和格林纳丁斯");
        countryMap.put("WSM", "萨摩亚");
        countryMap.put("SMR", "圣马力诺");
        countryMap.put("STp", "圣多美和普林西比");
        countryMap.put("SAU", "沙特阿拉伯");
        countryMap.put("SEN", "塞内加尔");
        countryMap.put("SYC", "塞舌尔");
        countryMap.put("SLE", "塞拉利昂");
        countryMap.put("SGP", "新加坡");
        countryMap.put("SVK", "斯洛伐克");
        countryMap.put("SVN", "斯洛文尼亚");
        countryMap.put("SLB", "所罗门群岛");
        countryMap.put("SOM", "索马里");
        countryMap.put("ZAF", "南非");
        countryMap.put("SGS", "南乔治亚岛和南桑德韦奇岛");
        countryMap.put("ESP", "西班牙");
        countryMap.put("LKA", "斯里兰卡");
        countryMap.put("SDN", "苏丹");
        countryMap.put("SUR", "苏里南");
        countryMap.put("SJM", "斯瓦尔巴群岛");
        countryMap.put("SWZ", "斯威士兰");
        countryMap.put("SWE", "瑞典");
        countryMap.put("CHE", "瑞士");
        countryMap.put("SYR", "叙利亚");
        countryMap.put("TJK", "塔吉克斯坦");
        countryMap.put("TZA", "坦桑尼亚");
        countryMap.put("THA", "泰国");
        countryMap.put("TGO", "多哥");
        countryMap.put("TKL", "托克劳");
        countryMap.put("TON", "汤加");
        countryMap.put("TTO", "特立尼达和多巴哥");
        countryMap.put("TUN", "突尼斯");
        countryMap.put("TUR", "土耳其");
        countryMap.put("TKM", "土库曼斯坦");
        countryMap.put("TCA", "特克斯科斯群岛");
        countryMap.put("TUV", "图瓦卢");
        countryMap.put("UGA", "乌干达");
        countryMap.put("UKR", "乌克兰");
        countryMap.put("ARE", "阿联酋");
        countryMap.put("GBR", "英国");
        countryMap.put("USA", "美国");
        countryMap.put("UMI", "美国本土外小岛屿");
        countryMap.put("URY", "乌拉圭");
        countryMap.put("UZB", "乌兹别克斯坦");
        countryMap.put("VUT", "瓦努阿图");
        countryMap.put("VAT", "梵蒂冈");
        countryMap.put("VEN", "委内瑞拉");
        countryMap.put("VNM", "越南");
        countryMap.put("VGB", "英属维尔京群岛");
        countryMap.put("VIR", "美属维尔京群岛");
        countryMap.put("WLF", "瓦利斯和富图纳");
        countryMap.put("ESH", "西撒哈拉");
        countryMap.put("YEM", "也门");
        countryMap.put("YUG", "南斯拉夫");
        countryMap.put("ZMB", "赞比亚");
        countryMap.put("ZWE", "津巴布韦");
    }
    protected String parseNation(int code) {
        String nation;
        switch (code) {
            case 1:
                nation = "汉";
                break;
            case 2:
                nation = "蒙古";
                break;
            case 3:
                nation = "回";
                break;
            case 4:
                nation = "藏";
                break;
            case 5:
                nation = "维吾尔";
                break;
            case 6:
                nation = "苗";
                break;
            case 7:
                nation = "彝";
                break;
            case 8:
                nation = "壮";
                break;
            case 9:
                nation = "布依";
                break;
            case 10:
                nation = "朝鲜";
                break;
            case 11:
                nation = "满";
                break;
            case 12:
                nation = "侗";
                break;
            case 13:
                nation = "瑶";
                break;
            case 14:
                nation = "白";
                break;
            case 15:
                nation = "土家";
                break;
            case 16:
                nation = "哈尼";
                break;
            case 17:
                nation = "哈萨克";
                break;
            case 18:
                nation = "傣";
                break;
            case 19:
                nation = "黎";
                break;
            case 20:
                nation = "傈僳";
                break;
            case 21:
                nation = "佤";
                break;
            case 22:
                nation = "畲";
                break;
            case 23:
                nation = "高山";
                break;
            case 24:
                nation = "拉祜";
                break;
            case 25:
                nation = "水";
                break;
            case 26:
                nation = "东乡";
                break;
            case 27:
                nation = "纳西";
                break;
            case 28:
                nation = "景颇";
                break;
            case 29:
                nation = "柯尔克孜";
                break;
            case 30:
                nation = "土";
                break;
            case 31:
                nation = "达斡尔";
                break;
            case 32:
                nation = "仫佬";
                break;
            case 33:
                nation = "羌";
                break;
            case 34:
                nation = "布朗";
                break;
            case 35:
                nation = "撒拉";
                break;
            case 36:
                nation = "毛南";
                break;
            case 37:
                nation = "仡佬";
                break;
            case 38:
                nation = "锡伯";
                break;
            case 39:
                nation = "阿昌";
                break;
            case 40:
                nation = "普米";
                break;
            case 41:
                nation = "塔吉克";
                break;
            case 42:
                nation = "怒";
                break;
            case 43:
                nation = "乌孜别克";
                break;
            case 44:
                nation = "俄罗斯";
                break;
            case 45:
                nation = "鄂温克";
                break;
            case 46:
                nation = "德昂";
                break;
            case 47:
                nation = "保安";
                break;
            case 48:
                nation = "裕固";
                break;
            case 49:
                nation = "京";
                break;
            case 50:
                nation = "塔塔尔";
                break;
            case 51:
                nation = "独龙";
                break;
            case 52:
                nation = "鄂伦春";
                break;
            case 53:
                nation = "赫哲";
                break;
            case 54:
                nation = "门巴";
                break;
            case 55:
                nation = "珞巴";
                break;
            case 56:
                nation = "基诺";
                break;
            case 57:
                nation = "穿青人";
                break;
            case 97:
                nation = "其他";
                break;
            case 98:
                nation = "外国血统中国籍人士";
                break;
            default:
                nation = "";
        }
        return nation;
    }
}
